package com.huaishushu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaishushu.common.BaseContext;
import com.huaishushu.common.R;
import com.huaishushu.entity.ShoppingCart;
import com.huaishushu.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加进购物车的商品信息：{}",shoppingCart);
        ShoppingCart cart = shoppingCartService.add(shoppingCart);

        return R.success(cart);
    }

    /**
     * 查询当前用户购物车内的商品信息
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        log.info("查询id为：{}  的用户购物车内的商品信息",BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.listShoppingCart();

        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);

        return R.success("购物车已清空");
    }




}
